package com.thread.thread.ThreadPool线程池;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class WorkStealingPoolTest {


	public static void main(String[] args) {

		//每个线程完成自己的任务后可以去获取其他线程的任务来执行
		ExecutorService service = Executors.newWorkStealingPool(2);

		int count = Runtime.getRuntime().availableProcessors();    //看cpu多少核的;如果是4核，默认就帮你起4个线程
		System.out.println(count);
		service.submit(()->{
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("线程1"+Thread.currentThread().getName());

		});

		service.submit(()->{
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("线程2"+Thread.currentThread().getName());

		});

		service.submit(()->{
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("线程3"+Thread.currentThread().getName());

		});
		try {
			System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
